<?php

namespace App\Http\Controllers;

use App\Exceptions\ShareRefererException;
use App\Internals\CourseInternal;
use App\Internals\UserInternal;
use App\Models\UserCourse;
use App\Models\UserLesson;
use App\Models\UserLessonDub;
use App\Models\UserStatus;
use FoxLib\Services\UrlParamsService;

class ShareController extends Controller
{
    /*
     * @api get /study-api/share/dub 跟读分享页数据
     * @version 0.0.1
     * @group web
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function dub()
    {
        try {
            $this->validate($this->request, [
                'refer' => 'required|string'
            ]);
            $ref = $this->request->input('refer');
            $refInfo = UrlParamsService::decode($ref);

            $userInfo = UserInternal::getByUserId($refInfo['userId']);
            $lessonInfo = CourseInternal::getLesson($refInfo['lessonId']);
            /** @var UserStatus $userStatus */
            $userStatus = UserStatus::where('user_id', $refInfo['userId'])->first();

            /** @var UserLesson $lessonObj */
            $lessonObj = UserLesson::ofUserLesson($refInfo['userId'], $refInfo['lessonId'])->first();

            $dubPages = CourseInternal::getLessonSection($refInfo['lessonId'], UserLesson::PROGRESS['dub']);
            $dubs = [];
            if ($dubPages) {
                foreach ($dubPages as $k => $item) {
                    /** @var UserLessonDub $dubObj */
                    $dubObj = UserLessonDub::ofUserPage($refInfo['userId'], $refInfo['lessonId'], $item['pageId'])->first();
                    if ($dubObj) {
                        $dubs[] = [
                            'dubAudio' => $dubObj->getUri(),
                            'duration' => $dubObj->getDuration(),
                            'image' => $item['image'],
                            'sentence' => $item['sentence']
                        ];
                    }
                }
            }
            $result = [
                'user' => [
                    'nickname' => $userInfo['nickname'],
                    'dubCount' => $userStatus->getDubCount()
                ],
                'lesson' => [
                    'cover' => $lessonInfo['cover'],
                    'name' => $lessonInfo['name'],
                    'audio' => $lessonInfo['audio'],
                    'star' => $lessonObj->getDubStar(),
                ],
                'pages' => $dubs
            ];
            return $this->sendSuccessJson($result);
        } catch (\Exception $e) {
            throw new ShareRefererException();
        }
    }

    /*
     * @api get /study-api/share/game 闯关分享页数据
     * @version 0.0.1
     * @group web
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function game()
    {
        try {
            $this->validate($this->request, [
                'refer' => 'required|string'
            ]);
            $ref = $this->request->input('refer');

            $refInfo = UrlParamsService::decode($ref);

            $userInfo = UserInternal::getByUserId($refInfo['userId']);

            $lessonInfo = CourseInternal::getLesson($refInfo['lessonId']);


            /** @var UserLesson $userLessonObj */
            $userLessonObj =  UserLesson::ofUserLesson($refInfo['userId'], $refInfo['lessonId'])->first();

            $result = [
                'user' => [
                    'avatar' => $userInfo['avatar'],
                    'nickname' => $userInfo['nickname']
                ],
                'lesson' => [
                    'cover' => $lessonInfo['cover'],
                    'name' => $lessonInfo['name'],
                    'wordCount' => $userLessonObj->getGameWordCount(),
                ],
                'game' => [
                    'gameTime' => UserLesson::convertTime($userLessonObj->getPlayGameTime()),
                    'isWinner' => UserLesson::isWinner($this->userId, $refInfo['lessonId'])
                ],
            ];
            return $this->sendSuccessJson($result);
        } catch (\Exception $e) {
            throw new ShareRefererException();
        }
    }

    /*
     * @api get /study-api/share/course 课程完成后分享页数据
     * @version 0.0.1
     * @group web
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function course()
    {
        try {
            $this->validate($this->request, [
                'refer' => 'required|string'
            ]);
            $ref = $this->request->input('refer');
            $refInfo = UrlParamsService::decode($ref);

            /** @var UserCourse $userCourseObj */
            $userCourseObj = UserCourse::ofUserCourse($refInfo['userId'], $refInfo['courseId'])->first();

            $userInfo = UserInternal::getByUserId($refInfo['userId']);

            // 取前5个
            $lessonIds = UserLesson::ofUserCourseOfficialLessons($refInfo['userId'], $refInfo['courseId'])->orderBy('id', 'asc')->take(5)->pluck('lesson_id');

            $lessonsInfo = CourseInternal::getLessons($lessonIds);

            $result = [
                'user' => [
                    'avatar' => $userInfo['avatar'],
                    'nickname' => $userInfo['nickname']
                ],
                'course' => [
                    'level' => $userCourseObj->getLevel(),
                    'star' => $userCourseObj->getStar(),
                    'dateCount' => $userCourseObj->getStudyDateCount(),
                    'lessonCount' => $userCourseObj->getLessonCount(),
                    'wordCount' => $userCourseObj->getWordCount(),
                    'dubCount' => $userCourseObj->getDubCount()
                ],
                'lessons' => array_values($lessonsInfo)
            ];
            return $this->sendSuccessJson($result);
        } catch (\Exception $e) {
            throw new ShareRefererException();
        }
    }
}
